package com.odp.service.impl;

import com.odp.entity.Comment;
import com.odp.mapper.CommentMapper;
import com.odp.mapper.PostMapper;
import com.odp.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private PostMapper postMapper;

    @Override
    //点赞评论（楼层）
    public boolean likeComment(int commentId) {
        int flag = commentMapper.likeComment(commentId);
        if(flag>0)
        {
            return true;
        }
        return false;
    }

    //评论
    @Override
    public boolean makeComment(Comment comment) {
        //真正需要的参数：帖子id   当前登录的用户id(session中拿)  评论内容        创建时间（set）
        //1.新建评论表项
        comment.setCreateTime(new Date());
        comment.setUserId(2);//session中取
        int flag1 = commentMapper.makeComment(comment);
        if(flag1<=0){
            return false;
        }
        //2.对应帖子回复数+1  未读数+1
        int postId = comment.getPostId();
        int flag2 = postMapper.postBeCommented(postId);
        if(flag2<=0){
            return false;
        }

        return true;
    }
}
